﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Activities;
using System.Data.Sql;
using System.Data;
using WFDataBaseProject.Helpers;
using System.Data.SqlClient;
using System.Diagnostics;
using System.ComponentModel;
using System.Activities.Presentation.Metadata;
using System.Activities.Presentation.PropertyEditing;
using WFDataBaseProject.Controls;

namespace WFDataBaseProject
{
    [Designer(typeof(UCProfileActivityDesigner))]
    public class ProfileActivity:CodeActivity
    {
        public ProfileActivity()
        {
            AttributeTableBuilder builder = new AttributeTableBuilder();
            builder.AddCustomAttributes(typeof(ProfileActivity), "SQLSERVER",
                    new EditorAttribute(typeof(ConnectionStringEditor),
                    typeof(DialogPropertyValueEditor)));
            MetadataStore.AddAttributeTable(builder.CreateTable());
        }

        #region arguments
            [Browsable(false)]
            public OutArgument<DataTable> AvailableObjects { get; set; }
            [Category("Inputs")]
            [RequiredArgument]
            [Description("The kind of SQL objects going to be requested")]
            public InArgument<ProfileHierarchy> Hierarchy { get; set; }
            [Category("Inputs")]
            [RequiredArgument]
            [Description("The connection to connect to the data base")]
            public InArgument<string> SQLSERVER { get; set; }
        #endregion

        

        #region private fields
        DataTable availableobjects;
        ProfileHierarchy hierarchy;
        string sqlserver;
#endregion
       
        
        protected override void Execute(CodeActivityContext context)
        {
            
            hierarchy = Hierarchy.Get<ProfileHierarchy>(context);
            sqlserver = SQLSERVER.Get<string>(context);
            switch (hierarchy)
            {
                case ProfileHierarchy.Instances:
                    availableobjects = AvailableObjects.Get<DataTable>(context);
                    break;
                case ProfileHierarchy.Databases:
                    {
                        GetInformation("Databases");
                        break;
                    }
                case ProfileHierarchy.Tables:
                    {
                        {
                            GetInformation("Tables");
                            break;
                        }
                    }
                case ProfileHierarchy.Views:
                    {
                        {
                            GetInformation("Views");
                            break;
                        }
                    }
                case ProfileHierarchy.StoredProcedures:
                    {
                        {
                            GetInformation("StoredProcedures");
                            break;
                        }
                    }
                case ProfileHierarchy.Functions:
                    {
                        {
                            GetInformation("Functions");
                            break;
                        }
                    }
                case ProfileHierarchy.Triggers:
                    {
                        {
                            GetInformation("Triggers");
                            break;
                        }
                    }
                case ProfileHierarchy.Assemblies:
                    {
                        {
                            GetInformation("Assemblies");
                            break;
                        }
                    }
                case ProfileHierarchy.Synonyms:
                    {
                        {
                            GetInformation("Synonyms");
                            break;
                        }
                    }
                case ProfileHierarchy.Indexes:
                    {
                        {
                            GetInformation("Indexes");
                            break;
                        }
                    }
                case ProfileHierarchy.Columns:
                    {
                        {
                            GetInformation("Columns");
                            break;
                        }
                    }
                default:
                    break;
            }
        }

        private void GetInformation(string pattern)
        {
            using (SqlConnection connection = new SqlConnection(sqlserver))
            {
                try
                {
                    connection.Open();
                    availableobjects = connection.GetSchema(pattern);

                }
                catch (SqlException caught)
                {
#if DEBUG
                    Debug.Print(caught.Message);
#endif
                }
                finally { connection.Close(); }


            }
        }
    }
}
